/**
 * 
 */
package com.nextive.interview.activity;

import java.util.List;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.nextive.interview.R;
import com.nextive.interview.dto.ContactDetails;
import com.nextive.interview.service.UserInfoService;
import com.nextive.interview.util.PhoneContactsUtil;

/**
 * @author Cesar Roman
 *
 */
public class ChooseContactActivity extends ListActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.contact_list);
		populateContactsInfo();
		
	}
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		
		super.onListItemClick(l, v, position, id);
		Intent intent = new Intent();
		
		ContactDetails selectedContactDetail = (ContactDetails) l.getItemAtPosition(position);
		intent.putExtra(UserInfoService.USER_INFO_KEYS.EMAIL.name(), selectedContactDetail.getEmail());
		setResult(RESULT_OK, intent);
		
		finish();
		
	}
	
	private void populateContactsInfo() {
		
		List<ContactDetails> emailDisplayArray = PhoneContactsUtil.generatePhoneContactsEmailArray(this);
		
		ArrayAdapter<ContactDetails> listAdapter = new ArrayAdapter<ContactDetails>(this, R.layout.contact_row, 
				R.id.contact_name_email, emailDisplayArray);
		
		setListAdapter(listAdapter);

	}
	
}
